package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.GroupCompanyAddDTO;
import org.jsola.hr.dto.GroupCompanyBatchDTO;
import org.jsola.hr.dto.GroupCompanyUpdateDTO;
import org.jsola.hr.query.GroupCompanyQuery;
import org.jsola.hr.service.IGroupCompanyService;
import org.jsola.hr.vo.CompanyVO;
import org.jsola.hr.vo.GroupCompanyListVO;
import org.jsola.hr.vo.GroupCompanyVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 子公司分管公司关系表
 *
 * @author zhr
 */
@RestController("hrGroupCompanyController")
@RequestMapping(HrConstants.API_PATH + "/v1/groupCompanies")
public class GroupCompanyController implements IGroupCompanyControllerAPI {

    @Autowired
    private IGroupCompanyService groupCompanyService;





    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody GroupCompanyBatchDTO groupCompanyBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<GroupCompanyAddDTO> groupCompanyAddDTOList = groupCompanyBatchDTO.getGroupCompanyAddDTOList();
        if (!CollectionUtils.isEmpty(groupCompanyAddDTOList)) {
            groupCompanyService.batchSave(groupCompanyAddDTOList,tokenUser);
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{groupCompanyId}")
    public Result<Integer> delete(@PathVariable String groupCompanyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除子公司分管公司关系表权限校验

        int count = groupCompanyService.deleteByIds(tokenUser, groupCompanyId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] groupCompanyIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除子公司分管公司关系表权限校验

        int count = groupCompanyService.deleteByIds(tokenUser, groupCompanyIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{groupCompanyId}")
    public Result<GroupCompanyVO> findById(@PathVariable String groupCompanyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看子公司分管公司关系表权限校验

        return Result.success(
            groupCompanyService.selectById(groupCompanyId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<GroupCompanyListVO>> listAll(@Valid GroupCompanyQuery groupCompanyQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看子公司分管公司关系表权限校验

        return Result.success(
            groupCompanyService.select(groupCompanyQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<GroupCompanyListVO>> page(@Valid GroupCompanyQuery groupCompanyQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            groupCompanyService.selectPage(groupCompanyQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/companyListByGroupId")
    public Result<Page<CompanyVO>> companyListByGroupId(@Valid GroupCompanyQuery groupCompanyQuery) {
        TokenUser tokenUser = UserContext.getUser();
        ParamException.notNull(groupCompanyQuery.getGroupId(), "分组id不能为空");
        // todo 查看项目权限校验

        return Result.success(
                groupCompanyService.companyListByGroupId(groupCompanyQuery, tokenUser)
        );
    }
}

